package com.ys.service.business.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.business.ThrOperateCompanyEuropeTrade;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.business.ThrOperateCompanyEuropeTradeMapper;
import com.ys.service.business.ThrOperateCompanyEuropeTradeService;
import com.ys.util.CheckUtil;
import com.ys.util.DateUtil;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 运营公司表欧洲贸易合规
 */
@Service("thrOperateCompanyEuropeTradeService")
public class ThrOperateCompanyEuropeTradeServiceImpl implements ThrOperateCompanyEuropeTradeService {
	private static final Logger logger = LoggerFactory.getLogger(ThrOperateCompanyEuropeTradeServiceImpl.class);
	
	@Autowired
	private ThrOperateCompanyEuropeTradeMapper thrOperateCompanyEuropeTradeMapper;


	@Autowired
	private CheckUtil checkUtil;


	//删除
	public void del(long id) throws Exception{
		thrOperateCompanyEuropeTradeMapper.deleteTrue(id);
	}



	/**
	 * 新增
	 */
	@Override
	public R add(Map<String, Object> params, Long menuId) throws Exception {
		logger.info("新增传参："+params);
		//参数验证
		String errorMsg = checkParams(params,menuId);
		if(errorMsg!=null && !"".equals(errorMsg)){
			return R.error(errorMsg);
		}
		ThrOperateCompanyEuropeTrade thrOperateCompanyEuropeTrade = JSON.parseObject(JSON.toJSONString(params), ThrOperateCompanyEuropeTrade.class);
		thrOperateCompanyEuropeTrade.setStatus(1);
		//计算VAT申报周期结束月份
		thrOperateCompanyEuropeTrade = countVatDeclareEnd(thrOperateCompanyEuropeTrade);

		thrOperateCompanyEuropeTradeMapper.insert(thrOperateCompanyEuropeTrade);
		//return R.ok(EditDto.closeAndReload());
		return R.ok(AddDto.gotoEdit("/web/business/thrOperateCompanyEuropeTrade/toEdit?id=" + thrOperateCompanyEuropeTrade.getId()));
	}


	//计算VAT申报周期结束月份
	@Override
	public ThrOperateCompanyEuropeTrade countVatDeclareEnd(ThrOperateCompanyEuropeTrade entity) throws Exception {
		if(checkUtil.isNotNullByObject(entity.getVatType()) && checkUtil.isNotNullByObject(entity.getVatDeclareStart())){
			if(entity.getVatType()==0){//0按月
				entity.setVatDeclareEnd(entity.getVatDeclareStart());
			}else if(entity.getVatType()==1){//1按季度
				Date startDate =  DateUtil.parseYYYY_MM(entity.getVatDeclareStart());
				Date endDate = DateUtil.addMonth(startDate,2);
				entity.setVatDeclareEnd(DateUtil.getYYYY_MM(endDate));
			}else if(entity.getVatType()==2){//2按年
				Date startDate =  DateUtil.parseYYYY_MM(entity.getVatDeclareStart());
				Date endDate = DateUtil.addMonth(startDate,11);
				entity.setVatDeclareEnd(DateUtil.getYYYY_MM(endDate));
			}
		}
		return entity;
	}


	/**
	 * 修改
	 */
	@Override
	public R update(Map<String, Object> params, Long menuId) throws Exception {
		logger.info("修改传参："+params);
		//参数验证
		String errorMsg = checkParams(params,menuId);
		if(errorMsg!=null && !"".equals(errorMsg)){
			return R.error(errorMsg);
		}
		ThrOperateCompanyEuropeTrade thrOperateCompanyEuropeTrade = JSON.parseObject(JSON.toJSONString(params), ThrOperateCompanyEuropeTrade.class);

		//计算VAT申报周期结束月份
		thrOperateCompanyEuropeTrade = countVatDeclareEnd(thrOperateCompanyEuropeTrade);

		thrOperateCompanyEuropeTradeMapper.update(thrOperateCompanyEuropeTrade);
		//return R.ok();
		return R.ok(EditDto.closeAndReload());
	}



	/**
	 * 参数验证
	 */
	public String checkParams(Map<String, Object> params, Long menuId) throws Exception {
		//获取pc字段数据
		List<YsCol> listField = new YsCol()
				.where("A.MenuId = "+menuId)
				.setOrderby("A.SortNo ASC")
				.queryList();
		//验证必填字段的非空；其他字段的长度限制、类型
		String backMsg = checkUtil.checkTableColLengthAndDataType("THR_OperateCompanyEuropeTrade",listField,params,ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1);
		return backMsg;
	}



}
